﻿using Microsoft.Extensions.Configuration;

namespace WebApi.Infrastructure
{
    public class AppSetting
    {
        public static IConfiguration Configuration { get; }
        static AppSetting()
        {
            Configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{System.Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
            .Build();
        }

        /// <summary>
        /// 获取指定Key的值
        /// </summary>
        /// <param name="key">Key(多级中间用:分割)</param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            return Configuration[key];
        }

        /// <summary>
        /// 获取指定Key的值，不存在或类型转换错误会抛出异常
        /// </summary>
        /// <param name="key">Key(多级中间用:分割)</param>
        /// <returns></returns>
        public static T GetValue<T>(string key)
        {
            return Configuration.GetValue<T>(key);
        }

        /// <summary>
        /// 获取指定Key的值，不存在或类型转换错误会返回默认值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">Key(多级中间用:分割)</param>
        /// <param name="objectValue"></param>
        /// <returns></returns>
        public static T GetValue<T>(string key, T objectValue)
        {
            return Configuration.GetValue<T>(key, objectValue);
        }

        /// <summary>
        /// 获取指定Key下键值对集合
        /// </summary>
        /// <param name="key">Key(多级中间用:分割)</param>
        /// <returns></returns>
        //public static IEnumerable<KeyValuePair<string, string>> GetSection(string key)
        //{
        //    var section = Configuration.GetSection(key).AsEnumerable().Where(w => w.Value != null);
        //    return section;
        //}
    }
}
